<template>
  <div class="box">
    <dv-border-box-10
      :style="{ '--right': right + 'px' }"
      backgroundColor="#00003bc1"
      :color="['#4789e3', '#65bfe5']"
      class="right-row-bg"
    >
      <div class="right-row-box">
        <div class="content">
          <div class="tools">
            <dv-decoration-7 :dur="2" class="title"
              >&nbsp;<font size="5">工具</font>&nbsp;</dv-decoration-7
            >
            <div class="tool">
              <i class="el-icon-close" @click="removeFn"></i>
            </div>
          </div>
          <div class="box-content">
            <h3 class="title">创建要素</h3>
            <div class="card">
              <div class="card-item" @click="drawFeatures('Point')">
                <img src="../../assets/icon/dot.png" class="image" />
                <p>点</p>
              </div>
              <div class="card-item" @click="drawFeatures('LineString')">
                <img src="../../assets/icon/xian_1.png" class="image" />
                <p>线段</p>
              </div>
              <div class="card-item" @click="drawFeatures('Polygon')">
                <img src="../../assets/icon/huaduobianxing.png" class="image" />
                <p>多边形</p>
              </div>
              <div class="card-item" @click="drawFeatures('Circle')">
                <img src="../../assets/icon/circle.png" class="image" />
                <p>圆形</p>
              </div>
            </div>
            <h3 class="title">测算工具</h3>
            <div class="card">
              <div class="card-item" @click="measureDistance('length')">
                <img src="../../assets/icon/calculate/测距.png" class="image" />
                <p>测距</p>
              </div>
              <div class="card-item" @click="measureDistance('area')">
                <img
                  src="../../assets/icon/calculate/测面积.png"
                  class="image"
                />
                <p>测面</p>
              </div>
              <div class="card-item" @click="endDraw1">
                <img
                  src="../../assets/icon/calculate/结束绘制.png"
                  class="image"
                />
                <p>结束测算</p>
              </div>
            </div>
            <h3 class="title">其它工具</h3>
            <div class="card">
              <div class="card-item" @click="dialogTableVisible = true">
                <img
                  src="../../assets/icon/calculate/书签管理.png"
                  class="image"
                />
                <p>书签管理</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </dv-border-box-10>
    <dv-border-box-12 v-show="dialogVisible" class="dialog">
      <p>配置项</p>
      <span>图层名称：<input type="text" v-model="options.title" /></span>
      <span
        >图层轮廓宽度：<input type="number" v-model="options.strokeWidth"
      /></span>
      <span
        >图层轮廓颜色：<input type="color" v-model="options.strokeColor"
      /></span>
      <span
        >图层填充颜色：<input type="color" v-model="options.fillColor"
      /></span>
      <div class="dialog-commit">
        <span>
          <input type="button" value="撤销" @click="undo" />
          &nbsp;
          <input type="button" value="清空" @click="draw.clear()" />
        </span>

        <input type="button" value="结束绘制" @click="endDraw" />
      </div>
    </dv-border-box-12>
    <el-dialog
      title="书签管理"
      :visible.sync="dialogTableVisible"
      :modal-append-to-body="false"
    >
      <el-table :data="bookmarks">
        <el-table-column
          property="name"
          label="名称"
          width="150"
        ></el-table-column>
        <el-table-column property="view.zoom" label="zoom" width="200">
        </el-table-column>
        <el-table-column property="view.center" label="center">
          <template slot-scope="scope">
            {{ scope.row.view.center[0] }}<br />{{ scope.row.view.center[1] }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import Draw from "ol/interaction/Draw.js";
import { Vector as VectorSource } from "ol/source";
import VectorLayer from "ol/layer/Vector";
import { Style, Stroke, Fill, Circle as CircleStyle } from "ol/style";
import MapTools from "@/utils/mapTools.js";
export default {
  name: "Test1LeftRow",
  data() {
    return {
      right: -500,
      dialogVisible: false,
      flag: "M4",
      openMap: Map,
      draw: null,
      a: null,
      options: {
        title: "",
        fillColor: "#766f76",
        strokeWidth: 2,
        strokeColor: "#a22a9e",
      },
      // 记录仪
      recorder: {
        Point: 0,
        LineString: 0,
        Polygon: 0,
        Circle: 0,
        Undefined: 0,
      },
      mapTools: null,
      dialogTableVisible: false,
      bookmarks: [],
    };
  },
  props: {
    Map: {
      type: Object,
      requered: true,
    },
  },
  mounted() {},
  computed: {
    getMenuFlag() {
      return this.$store.state.activeMenu;
    },
    getBookmarks() {
      return this.$store.state.bookmarks;
    },
  },
  watch: {
    getMenuFlag: {
      handler(newValue, oldValue) {
        // console.log(newValue, oldValue);
        this.right = -500;
        if (newValue === this.flag) {
          this.right = 0;
        }
      },
      deep: true,
      immediate: true,
    },
    getBookmarks: {
      handler(newVal) {
        this.bookmarks = newVal;
      },
      deep: true,
      immediate: true,
    },
  },

  methods: {
    removeFn() {
      console.log("removeFn");
      this.right = -500;
    },
    drawFeatures(type) {
      let t = "";
      try {
        this.recorder[type] += 1;
        t = type + " " + this.recorder[type];
      } catch (error) {
        this.recorder.Undefined += 1;
        t = "Undefined" + this.recorder.Undefined;
      }

      this.options.title = t;

      this.dialogVisible = true;

      this.Map.removeInteraction(this.draw);
      this.addInteraction(type);
    },
    addInteraction(type) {
      const source = new VectorSource({ wrapX: false });

      const vector = new VectorLayer({
        source: source,
        isRemove: false,
        zIndex: 999,
        title: this.options.title,
        name: "draw feacture",
        type: type,
        style: new Style({
          fill: new Fill({
            color: this.options.fillColor + "96", // 为了填充透明色 所以‘96’加上 就变成了'#******96'
          }),
          stroke: new Stroke({
            color: this.options.strokeColor,
            width: this.options.strokeWidth,
          }),
          image: new CircleStyle({
            radius: 8,
            // fill: new Fill({
            //   color: "orange",
            // }),
            fill: new Fill({
              color: "red",
            }),
            stroke: new Stroke({
              color: "blak",
              width: 5,
            }),
          }),
        }),
      });
      this.Map.addLayer(vector);

      if (type) {
        this.draw = new Draw({
          source: source,
          type: type,
          style: new Style({
            fill: new Fill({
              color: this.options.fillColor + "96",
            }),
            stroke: new Stroke({
              color: this.options.strokeColor,
              width: this.options.strokeWidth,
            }),
            image: new CircleStyle({
              radius: 5,
              fill: new Fill({
                color: "orange",
              }),
            }),
          }),
        });
        this.Map.addInteraction(this.draw);
      }

      this.draw.on("drawend", (e) => {
        let l = {
          title: vector.get("title"),
          name: vector.get("name"),
          uid: vector.ol_uid,
          id: new Date().getTime(),
          visible: vector.get("visible"),
          type: vector.get("type"),
          edit: true,
          delete: true,
          layer: vector,
          children: [],
        };
        let layeres = this.$store.getters.getLayeres;
        layeres.ul.push(l);

        this.$store.commit("setLayeres", layeres);
      });
    },
    undo() {
      this.draw.removeLastPoint();
    },
    endDraw() {
      this.dialogVisible = false;
      this.Map.removeInteraction(this.draw);
    },
    measureDistance(v) {
      if (!this.mapTools) this.mapTools = new MapTools(this.Map);
      this.mapTools.measureDistance(v);
    },
    endDraw1() {
      if (!this.mapTools)
        return this.$notify.warning({
          title: "警告",
          message: "没有绘制任务，请先开始绘制",
        });
      this.mapTools.endDraw();
      this.mapTools = null;
    },
    handleDelete(v) {
      if (!v) return;
      this.bookmarks = this.bookmarks.filter((item) => item.id != v.id);
      this.$store.commit("setBookmarks", this.bookmarks);
    },
  },
};
</script>

<style lang="less" scoped>
.right-row-bg {
  position: absolute;
  top: 0;
  right: var(--right);
  bottom: 0;
  width: 20%;
  margin: 5px;
  z-index: 9;
  backdrop-filter: saturate(150%) blur(8px);
  -webkit-backdrop-filter: saturate(150%) blur(8px);
  animation: run 1s linear;
  color: #1dc1f5;
}

@keyframes run {
  0% {
    left: -100%;
  }

  100% {
    left: 0;
  }
}

.right-row-box {
  position: absolute;

  top: 0;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  // color: #4789e3;
  // color: #3f96a5;

  .border-box {
    width: 95%;
    height: 150px;
  }

  // .tolbar {
  //     width: 100%;
  //     height: 5%;
  // }

  .content {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: flex-start;
    width: 100%;
    height: 100%;
  }
  .box-content {
    .title {
      text-align: left;
      margin: 10px;
      padding-left: 5px;
      border-left: 5px #139fc0 solid;
    }
    .card {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: flex-start;

      .card-item {
        width: 30%;
        height: 100px;
        border-radius: 8px;
        margin-bottom: 10px;
        margin-left: 8px;
        background-color: #2f338a;
        transition: 0.3s linear;

        &:hover {
          scale: 1.05;
        }
        p {
          margin: 0;
          text-align: center;
        }
      }
    }
  }
  .box-content:nth-child(1n) {
    flex: 2;
  }

  .box-content:nth-child(2n) {
    flex: 1;
  }
}

.tools {
  // display: flex;
  // flex-direction: row;
  // flex-wrap: nowrap;
  // justify-content: space-around;
  // align-items: center;
  position: relative;
  width: 100%;
  margin-bottom: 5px;

  .title {
    width: 100%;
    height: 30px;
  }

  .tool {
    position: absolute;
    right: 10px;
    top: 5px;
    background: #4687e240;
  }
}

.el-descriptions {
  background-color: transparent !important;
  color: #1dc1f5 !important;
  font-size: 18px;
}

.margin-left {
  margin-left: 10px;
}
</style>
<style lang="less">
.dialog {
  position: absolute !important;
  width: 200px !important;
  height: 250px !important;
  right: 20.5%;
  bottom: 70px;
  background-color: #000b63;
  backdrop-filter: saturate(150%) blur(8px);
  -webkit-backdrop-filter: saturate(150%) blur(8px);
  color: #1dc1f5;

  span {
    display: block;
    text-align: start;
    margin: 5px 10px;
    input {
      width: 60px;
    }
    input[type="number"] {
      width: 50px;
    }
    input[type="text"] {
      width: 90px;
    }
  }

  .dialog-commit {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-evenly;
  }
}
</style>
